package com.jinyi.wwdeng.autodialer.data

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update

@Dao
interface CallTaskDao {
    /* 插入数据 */
    @Insert
    suspend fun insert(phoneNumber: CallTaskEntity): Long

    /* 更新数据 */
    @Update
    suspend fun update(phoneNumber: CallTaskEntity)

    /* 根据状态获取数据列表 */
    @Query("SELECT * FROM tab_call_tasks WHERE status = :status ORDER BY id ASC")
    suspend fun getNumbersByStatus(status: Int): List<CallTaskEntity>

    /* 获取待拨号数据 - 指定数量 */
    @Query("SELECT * FROM tab_call_tasks WHERE status = 'DIAL_PENDING' ORDER BY create_time ASC LIMIT :limit")
    suspend fun getPendingTasks(limit: Int): List<CallTaskEntity>

    /* 获取所有待拨号数据 */
    @Query("SELECT * FROM tab_call_tasks WHERE status = 'DIAL_PENDING' ORDER BY create_time ASC")
    suspend fun getPendingTasks(): List<CallTaskEntity>

    /* 获取待上传数据 */
    @Query("SELECT * FROM tab_call_tasks WHERE status = 'UPLOAD_PENDING' AND local_audio_path IS NOT NULL ORDER BY create_time ASC LIMIT :limit")
    suspend fun getUploadPendingTasks(limit: Int): List<CallTaskEntity>

    /* 获取待上报数据 */
    @Query("SELECT * FROM tab_call_tasks WHERE status = 'UPLOAD_SUCCESS' AND server_audio_path IS NOT NULL AND local_audio_path IS NOT NULL ORDER BY create_time ASC LIMIT :limit")
    suspend fun getReportPendingTasks(limit: Int): List<CallTaskEntity>

    /* 获取拨号中数据 */
    @Query("SELECT * FROM tab_call_tasks WHERE status = 'DIAL_DIALING'")
    suspend fun getDialingTasks(): List<CallTaskEntity>

    /* 根据id，更新状态 */
    @Query("UPDATE tab_call_tasks SET status = :status WHERE id = :taskId")
    suspend fun updateStatus(taskId: Long, status: String)

    /* 根据id，更新状态和错误信息 */
    @Query("UPDATE tab_call_tasks SET status = :status, error_message = :errorMessage WHERE id = :taskId")
    suspend fun updateStatusWithError(taskId: Long, status: String, errorMessage: String?)

    /* 根据id，更新状态和录音本地路径 */
    @Query("UPDATE tab_call_tasks SET status = :status, local_audio_path = :localPath WHERE id = :taskId")
    suspend fun updateStatusWithLocalPath(taskId: Long, status: String, localPath: String)

    /* 根据id，更新状态和录音服务器路径 */
    @Query("UPDATE tab_call_tasks SET status = :status, server_audio_path = :serverPath WHERE id = :taskId")
    suspend fun updateStatusWithServerPath(taskId: Long, status: String, serverPath: String)

    /* 根据id，更新文件上传重试次数 */
    @Query("UPDATE tab_call_tasks SET upload_retry_count = upload_retry_count + 1 WHERE id = :taskId")
    suspend fun incrementUploadRetryCount(taskId: Long)

    /* 根据id，更新数据上报重试次数 */
    @Query("UPDATE tab_call_tasks SET report_retry_count = report_retry_count + 1 WHERE id = :taskId")
    suspend fun incrementReportRetryCount(taskId: Long)

    /* 根据手机号码，获取数据 */
    @Query("SELECT * FROM tab_call_tasks WHERE phone_number = :phoneNumber AND status = 'DIALING' LIMIT 1")
    suspend fun getDialingTaskByNumber(phoneNumber: String): CallTaskEntity?

    /* 获取待拨号数量 */
    @Query("SELECT COUNT(*) FROM tab_call_tasks WHERE status = 'DIAL_PENDING'")
    suspend fun getPendingCount(): Int

    /* 获取所有号码列表 */
    @Query("SELECT * FROM tab_call_tasks ORDER BY id DESC")
    suspend fun getAllTasks(): MutableList<CallTaskEntity>

    /* 根据手机号码获取数据对象 */
    @Query("SELECT * FROM tab_call_tasks WHERE phone_number = :phoneNumber")
    suspend fun getNumberByPhone(phoneNumber: String): CallTaskEntity?

    /* 删除已完成的数据： timestamp指定时间之前 */
    @Query("DELETE FROM tab_call_tasks WHERE status = 'COMPLETED' AND create_time < :timestamp")
    suspend fun deleteCompletedTasksBefore(timestamp: Long)

    /* 清空所有数据 */
    @Query("DELETE FROM tab_call_tasks")
    suspend fun deleteAll()
}